package com.example.pathtracker.activity

import android.content.Intent
import android.os.Bundle
import android.os.SystemClock
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.pathtracker.R

class MainActivity : AppCompatActivity() {
    private var lastBackPressTime: Long = 0
    private val backPressInterval: Long = 2000 // 两次点击间隔时间，单位毫秒

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView = findViewById<RecyclerView>(R.id.rv_functions)
        recyclerView.layoutManager = GridLayoutManager(this, 2)
        recyclerView.adapter = FunctionAdapter(getFunctionList())

        // 设置跑马灯效果
        val tvMarquee = findViewById<TextView>(R.id.tv_marquee)
        tvMarquee.isSelected = true
    }

    private fun getFunctionList(): List<FunctionItem> {
        return listOf(
            FunctionItem("路径采集", R.mipmap.icon_lujinglujing) {
                startActivity(Intent(this, MapActivity::class.java))
            },
            FunctionItem("点位采集", R.mipmap.icon_dingwei) {
                startActivity(Intent(this, PointMarkActivity::class.java))
            },
            FunctionItem("地图总览", R.mipmap.icon_ditu) {
                startActivity(Intent(this, MapOverviewActivity::class.java))
            },
            FunctionItem("离线地图", R.mipmap.lixianditu) {
                startActivity(Intent(this, com.amap.api.maps.offlinemap.OfflineMapActivity::class.java))
            },
            // 后续可继续添加
            FunctionItem("系统介绍", R.mipmap.icon_system) {
                startActivity(Intent(this, SystemIntroActivity::class.java))
            },
            FunctionItem("数据统计", R.mipmap.icon_tj) {
                startActivity(Intent(this, DataStatsActivity::class.java))
            }
        )
    }

    override fun onBackPressed() {
        if (SystemClock.elapsedRealtime() - lastBackPressTime < backPressInterval) {
            super.onBackPressed()
        } else {
            Toast.makeText(this, "再按一次返回键退出应用", Toast.LENGTH_SHORT).show()
            lastBackPressTime = SystemClock.elapsedRealtime()
        }
    }
}

data class FunctionItem(val title: String, val iconRes: Int, val onClick: () -> Unit)

class FunctionAdapter(private val items: List<FunctionItem>) :
    RecyclerView.Adapter<FunctionAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: android.view.ViewGroup, viewType: Int): ViewHolder {
        val view = android.view.LayoutInflater.from(parent.context)
            .inflate(R.layout.item_function, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount() = items.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(items[position])
    }

    inner class ViewHolder(itemView: android.view.View) : RecyclerView.ViewHolder(itemView) {
        private val ivIcon: ImageView = itemView.findViewById(R.id.iv_icon)
        private val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        fun bind(item: FunctionItem) {
            ivIcon.setImageResource(item.iconRes)
            tvTitle.text = item.title
            itemView.setOnClickListener { item.onClick() }
        }
    }
} 